package course.ya.doc.from.code;

import course.ya.doc.from.code.model.statements.IfStatement;
import java.io.IOException;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws IOException {
        System.out.println("Hello World!");
        Front ft = new Front("package course.ya.doc.from.code.model.lexer.project;\n" +
"\n" +
"import java.io.BufferedReader;\n" +
"import java.io.FileNotFoundException;\n" +
"import java.io.FileReader;\n" +
"import java.io.IOException;\n" +
"import java.util.StringTokenizer;\n" +
"\n" +
"public class Scanner {\n" +
"\n" +
"    private String nameOfFile;\n" +
"    private String data;\n" +
"    private SyntaxTree resultTree;\n" +
"    private String useDelimiters;\n" +
"    private String[] delimiters;\n" +
"\n" +
"    public Scanner(String data,String[] delimiters) {\n" +
"        this.nameOfFile = \"\";\n" +
"        this.resultTree = new SyntaxTree();\n" +
"        StringBuilder sb=new StringBuilder();\n" +
"        for(int i=0;i<delimiters.length;i++)\n" +
"        {\n" +
"            sb.append(delimiters[i]);\n" +
"        }\n" +
"        this.useDelimiters = sb.toString();\n" +
"        this.delimiters=delimiters;\n" +
"        this.data = data;\n" +
"    }\n" +
"\n" +
"    public Scanner(String nameOfFile, SyntaxTree Tree,String[] delimiters) throws IOException {\n" +
"        this.nameOfFile = nameOfFile;\n" +
"        this.resultTree = Tree;\n" +
"        StringBuilder sbuilder=new StringBuilder();\n" +
"        for(int i=0;i<delimiters.length;i++)\n" +
"        {\n" +
"            sbuilder.append(delimiters[i]);\n" +
"        }\n" +
"        this.useDelimiters = sbuilder.toString();\n" +
"        this.delimiters=delimiters;\n" +
"        BufferedReader reader\n" +
"                = new BufferedReader(new FileReader(this.nameOfFile));\n" +
"\n" +
"        StringBuilder sb = new StringBuilder();\n" +
"        String line = null;\n" +
"        while ((line = reader.readLine()) != null) {\n" +
"            sb.append(line).append(\"\\n\");\n" +
"        }\n" +
"\n" +
"        reader.close();\n" +
"        this.data = sb.toString();\n" +
"    }\n" +
"\n" +
"    public void Scan() {\n" +
"        String token = null;\n" +
"        String nextToken = null;\n" +
"        int low = 0;\n" +
"        StringTokenizer st\n" +
"                = new StringTokenizer(this.data, this.useDelimiters, true);\n" +
"\n" +
"        while (st.hasMoreTokens()) {\n" +
"            token = st.nextToken();\n" +
"            token = FindComment(token);\n" +
"            if (st.hasMoreTokens()) {\n" +
"                nextToken = st.nextToken();\n" +
"            } else {\n" +
"                nextToken = this.delimiters[0];\n" +
"            }\n" +
"            if (FindAnomaly(token) == 3) {\n" +
"                if(token.indexOf(\":\")==-1)\n" +
"                {\n" +
"                StringBuilder sb = new StringBuilder();\n" +
"                int balans = 0;\n" +
"                do {\n" +
"                    if (token.indexOf(\"(\") > 0) {\n" +
"                        balans++;\n" +
"                    } else {\n" +
"                        if (token.indexOf(\")\") > 0) {\n" +
"                            balans--;\n" +
"                        }\n" +
"                    }\n" +
"                    if (!token.equals(this.delimiters[0])) {\n" +
"                        sb.append(token).append(this.delimiters[0]);\n" +
"                    }\n" +
"                    token = st.nextToken();\n" +
"                    token = token.trim();\n" +
"\n" +
"                } while (balans != 0);\n" +
"                nextToken = token;\n" +
"                token = sb.toString();\n" +
"                token = token.substring(0, token.length() - 1);\n" +
"                token = token.trim();\n" +
"                }\n" +
"            }\n" +
"            if ((FindAnomaly(token) == 1 || FindAnomaly(token) == 2 || FindAnomaly(token) == 3) && nextToken.equals(this.delimiters[0])) {\n" +
"                RestructAnomaly(token);\n" +
"            } else {\n" +
"                if (nextToken.equals(this.delimiters[0])) {\n" +
"                    if (!token.equals(\"\")) {\n" +
"                        this.resultTree.Insert(token, false, false);\n" +
"                    }\n" +
"                } else {\n" +
"                    if (nextToken.equals(this.delimiters[1])) {\n" +
"                        low++;\n" +
"                        if (!token.equals(\"\")) {\n" +
"                            this.resultTree.Insert(token, false, true);\n" +
"                        }\n" +
"                    } else {\n" +
"                        if (nextToken.equals(this.delimiters[2])) {\n" +
"                            low--;\n" +
"                            if (!token.equals(\"\")) {\n" +
"                                this.resultTree.Insert(token, false, false);\n" +
"                            }\n" +
"                            this.resultTree.JumpUp();\n" +
"                        }\n" +
"                    }\n" +
"                }\n" +
"            }\n" +
"\n" +
"        }\n" +
"    }\n" +
"\n" +
"    private String FindComment(String lex) {\n" +
"        lex = lex.trim();\n" +
"        if (!lex.equals(\"\")) {\n" +
"            char[] mas = lex.toCharArray();\n" +
"            int i = 0;\n" +
"            if (mas[0] == '/' && mas[1] == '/') {\n" +
"                while (i < lex.length() && mas[i] != '\\n') {\n" +
"                    i++;\n" +
"                }\n" +
"                String Comment = lex.substring(0, i);\n" +
"\n" +
"                this.resultTree.Insert(Comment, false, false);\n" +
"\n" +
"                lex = lex.substring(i, lex.length());\n" +
"                lex.trim();\n" +
"            } else {\n" +
"                if (mas[0] == '/' && mas[1] == '*') {\n" +
"                    while (mas[i] != '*' && mas[i + 1] != '/' && i < lex.length()) {\n" +
"                        i++;\n" +
"                    }\n" +
"                    String Comment = lex.substring(0, i);\n" +
"\n" +
"                    this.resultTree.Insert(Comment, false, false);\n" +
"\n" +
"                    lex = lex.substring(i, lex.length());\n" +
"                    lex.trim();\n" +
"                }\n" +
"            }\n" +
"        }\n" +
"        return lex;\n" +
"\n" +
"    }\n" +
"\n" +
"    private int FindAnomaly(String word) {\n" +
"        StringTokenizer st = new StringTokenizer(word, \" ()\");\n" +
"        String analys = null;\n" +
"        if (st.hasMoreTokens()) {\n" +
"            analys = st.nextToken();\n" +
"            analys = analys.trim();\n" +
"        }\n" +
"        if (analys != null) {\n" +
"            if (analys.equals(\"if\")) {\n" +
"                return 1;\n" +
"            }\n" +
"            if (analys.equals(\"while\")) {\n" +
"                return 2;\n" +
"            }\n" +
"            if (analys.equals(\"for\")) {\n" +
"                return 3;\n" +
"            }\n" +
"        }\n" +
"        return 0;\n" +
"\n" +
"    }\n" +
"\n" +
"    private void RestructAnomaly(String token) {\n" +
"        int balans = 0;\n" +
"        boolean flag = false;\n" +
"        char[] tokenMas = token.toCharArray();\n" +
"        int index = 0;\n" +
"        for (index = 0; index < tokenMas.length; index++) {\n" +
"            if (tokenMas[index] == '(') {\n" +
"                balans++;\n" +
"                flag = true;\n" +
"            } else {\n" +
"                if (tokenMas[index] == ')') {\n" +
"                    balans--;\n" +
"                }\n" +
"            }\n" +
"            if (balans == 0 && flag == true) {\n" +
"                break;\n" +
"\n" +
"            }\n" +
"        }\n" +
"        String startToken = token.substring(0, index + 1);\n" +
"        startToken = startToken.trim();\n" +
"        String endToken = token.substring(index + 1);\n" +
"        endToken = endToken.trim();\n" +
"        this.resultTree.Insert(startToken, false, true);\n" +
"        if (!endToken.equals(\"\")) {\n" +
"            this.resultTree.Insert(endToken, false, false);\n" +
"            this.resultTree.JumpUp();\n" +
"        }\n" +
"\n" +
"    }\n" +
"\n" +
"    public SyntaxTree ReturnTree() {\n" +
"        return this.resultTree;\n" +
"    }\n" +
"}\n");
        ft.run();
        // IfStatement i_st = new IfStatement("if (i<n) {i++; a--; int b = 2 + 3;}");
        int k = 0;

    }
}
